var id;
var urlType = ly.getUrlParam("type");
var webSiteArr = []; // 服务网点

$(function() {
  id = ly.getUrlParam("id");

  getWebSite(); // 获取服务网点

  if (urlType == 1) {
    // 新增
    $("#ifSuperMan").getSelect({
      type: "radio",
      asName: "ifSuperMan",
      list: [{ "value": "0", "display": "普通管理员" }, { "value": "1", "display": "超级管理员" }]
    })
  } else {
    request({
      url: site.apiUrl + '/admin/getAdmin',
      type: "post",
      data: { id: id },
      callback: function(f) {
        if (f.code == 200) {
          var data = f.data;
          $("#name").val(data.name);
          $("#adminName").val(data.adminName);
          $("#passward").val(data.passward);
          $("#remark").val(data.remark);
          $("#webSite").getSelect({
            asName: "webSite",
            type: "select",
            list: webSiteArr,
            default: data.webSite
          });
          $("#ifSuperMan").getSelect({
            type: "radio",
            asName: "ifSuperMan",
            list: [{ "value": "0", "display": "普通管理员" }, { "value": "1", "display": "超级管理员" }],
            default: data.ifSuperMan
          })
        } else {
          ly.toast(f.desc);
        }
      }
    })
  }
  //提交
  $("body").on("click", "#save-btn", function(event) {
    event.preventDefault();
    var success = $('.form').validate('submitValidate');
    if (success) {
      //验证通过提交表单
      submitFom();
    }
  })

});

// 获取服务网点
function getWebSite() {
  webSiteArr = [];
  request({
    url: site.apiUrl + '/webInfo/selectAllWebInfo',
    type: "get",
    callback: function(f) {
      if (f.code == 200) {
        var forData = f.data;
        if (forData.length > 0) {
          for (var i = 0; i < forData.length; i++) {
            webSiteArr.push({
              value: forData[i].id,
              display: forData[i].webName
            })
          }
        } else {
          webSiteArr.push({value: "", display: "暂无分类"})
        }
        $("#webSite").getSelect({
          asName: "webSite",
          type: "select",
          list: webSiteArr
        });
      }
    }
  })
}

/*
 * 提交表单
 */
function submitFom() {
  var name = $("#name").val();
  var adminName = $("#adminName").val();
  var passward = $("#passward").val();
  var remark = $("#remark").val();
  var webSite = $("#webSite").val();
  var ifSuperMan = $("#ifSuperMan").find("input[name=ifSuperMan]:checked").val();

  var obj = {};
  var url = site.apiUrl + "/admin/addAdmin";
	obj = {
		'name': name,
    'adminName': adminName,
    'passward': passward,
    'remark': remark,
    'webSite': webSite,
    'ifSuperMan': ifSuperMan
  };
  // 修改
  if (urlType == 2) {
    obj.id = id;
  }
  request({
    url: url,
    data: obj,
    type: "post",
    ajaxform: "true",
    callback: function(f) {
      if (f.code == 200) {
        if (urlType == 1) {
          ly.alert('保存成功', function() {
            ly.goto("list.html");
          });
        } else {
          ly.alert('编辑成功', function() {
            ly.goto("list.html");
          });
        }
      } else {
        ly.toast(f.msg);
      }
    }
  });
}